<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.Enumeration,java.util.ArrayList,java.util.HashMap,java.util.HashSet,cn.edu.nju.ws.aquila.util.QueryUtil" %>
<%@ page import="cn.edu.nju.ws.aquila.query.Relation,cn.edu.nju.ws.aquila.query.Query,cn.edu.nju.ws.aquila.query.InstantQuery,cn.edu.nju.ws.aquila.query.Datasource,cn.edu.nju.ws.aquila.user.User" %> 
<%@ page import="java.net.URLEncoder" %>       
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Aquila Query Search</title>
<link rel="stylesheet" type="text/css" href="css/result.css" />
<script type="text/javascript" src="script/jquery.js"></script>
<script type="text/javascript" src="script/config.js"></script>	
</head>
<body>
<div id="top">
<%
String mquery=null;
if(request.getAttribute("query")==null){
	response.sendRedirect("index.jsp") ;
	return;
}
else
	mquery = (String)request.getAttribute("query");
 int totalNum=0;   
if(request.getAttribute("totalNum")==null){
	response.sendRedirect("index.jsp") ;
	return;
}
else
	totalNum = ((Integer)request.getAttribute("totalNum")).intValue();
int totalPage = totalNum/10+1;
int currentPage = 1;
if(request.getAttribute("currentPage")==null){
	response.sendRedirect("index.jsp") ;
	return;
}
else
	currentPage = ((Integer)request.getAttribute("currentPage")).intValue();
ArrayList<Query> queryList  = null;
if(request.getAttribute("queryList")==null){
	response.sendRedirect("index.jsp") ;
	return;
}
else
	queryList = (ArrayList<Query>)request.getAttribute("queryList");
double time = 0;
if(request.getAttribute("time")==null){
	response.sendRedirect("index.jsp") ;
	return;
}
else
	time = (Double)request.getAttribute("time");
%>
	<form id="query_form"  method="get" action="searchq">
				 <div>
           <input id="query" type="text" size="55" value="<%=mquery %>" title="Aquila Query Search" name="query">
           <input type="submit" value="Search Queries" />
          </div>
        </form>
				<div id="appbar">
	       <div id="resultStats">
	       <%if(currentPage==1){ %>
	            About <%=totalNum%> results (<%=time %> seconds)
	       <%} else {%>
	           Page <%=currentPage%> of <%=totalNum%> results (<%=time %> seconds)
	        <%} %>
	       </div>
	      </div>
			</div>
			<div id="main">
				<div id="result">
					<ol>
					<%
					   for(int i=0;i<queryList.size();i++){	
						   int id=0;
						   Query query  = queryList.get(i) ;
						   InstantQuery instQuery = null;
						   boolean instFlag = false;
						   if(query instanceof InstantQuery){
							   instFlag = true;
						   }
						   id = Query.getQueryIdByNameAuthor(query.getName(),query.getAuthor()) ;
						   int dependentQueryId = 0;
						   if(instFlag){
							   instQuery =(InstantQuery)query;
							   id = instQuery.getInstantQueryId();
							   dependentQueryId = instQuery.getDependentQueryId();
						   }
						   String title = query.getTitle();
						   String desc =query.getDesc();
						   int argNum = query.getAllArguments().size();
						   String instTitle= title;
						  //处理标题替换
						   if(instFlag)
							   instTitle = QueryUtil.instantiateTitleRetainFlag(instTitle,instQuery.getValues()) ;
							for(int z=0;z<argNum;z++){
								String str="@" ;
								for(int j=0;j<z+1;j++){
									str+="\\$";
								}
								str+="@";
								String	replacement =null;
								if(!instFlag){
								   replacement= "<span class='param' pos='"+z+"'>";
								}else
								   replacement= "<span class='inst-param' pos='"+z+"'>";
								title = title.replaceFirst(str, replacement);
								title= title.replaceFirst(str, "</span>") ;
								instTitle = instTitle.replaceFirst(str, replacement);
								instTitle= instTitle.replaceFirst(str, "</span>") ;
							}
						
					%>
						<li>
						  <div class="title-info ellipsis">
					<%if(instFlag) {
						String href="answer?type=inst&id="+id;
					%>
						     <a class="title" type="inst" datatypeid="<%=id%>" href="<%=href%>"><%=instTitle %></a>
					<%}else{ %>	
					  	     <a class="title" type="tmpt" datatypeid="<%=id%>" href="#"><%=title %></a>
					<%} %>		 
						   </div>
							 <div class="summary">
							 <div class="prop"><span class="prop-type">Description: </span><%=desc %></div>
							 <div class="prop"><span class="prop-type">Datasources: </span><ul class="display-inline">
							 <%
							    ArrayList<Datasource> datasources  = query.getDatasets();
							    for(int k=0;k<datasources.size();k++){
							    	Datasource datasource = datasources.get(k);
							    	int dsId = datasource.getDsId();
							    	//任意不存在的作者
							    	HashSet<String> tags= datasource.getTags();
							    	String tag = tags.iterator().next();
							    	
							 %>
							      <li class="tag" datatypeid="<%=dsId%>"><%=tag %></li>
							  <% if(k!=datasources.size()-1){ %>
							        ,
							   <%}} %>
							 </ul></div>
							 <%if(instFlag) {%>
							 <div class="prop ellipsis"><span class="prop-type">InstanceOf: </span> <a class="instance-title" type="tmpt" href="#" datatypeid="<%=dependentQueryId%>"><%=title %></a></div>
							 </div>
							 <%} %>
						</li>
						<%} %>
				  </ol>	
				</div>
				<div id="navfoot">
				  <%if(currentPage>1){
					  String link =URLEncoder.encode(mquery,"utf-8") ;
					  String href = "searchq?page="+(currentPage-1)+"&query="+link ;
				%>  
					 <a  class='nav pnnav' href="<%=href%>">Prev</a>
					 <%} 
					     //显示10页
					     int endPage = totalPage>(currentPage+4)?(currentPage+4):totalPage;
					     int startPage = (currentPage-4)>0?(currentPage-4):1;
					     for(int i=startPage;i<=endPage;i++){
					    	 String link =URLEncoder.encode(mquery,"utf-8") ;
					    	  String href = "searchq?page="+i+"&query="+link ;
					 %>
					  <%if(i==currentPage){ %>
					     <span class='nav current' ><%=i %></span>
					   <%}else{ %>
					      <a class='nav' href="<%=href %>">i</a>
                     <%}} %>
				  <%if(currentPage<(totalPage)){
					  String link =URLEncoder.encode(mquery,"utf-8") ;
					  String href ="searchq?page="+(currentPage+1)+"&query="+link ; 
				%>  
					 <a  class='nav pnnav' href="<%=href%>">Next</a>
					 <%} %>
				</div>
		  </div>
		<div id="footer"></div>
</body>
</html>